/*-//GEN-BEGIN:copyright
 * ForBILE - Framework for Building Interactive Learning Environments
 * Computing Institute - Federal University of Alagoas - Brazil
 * Group of Otimization of the Web
 *
 * Copyright (C) 2007 Ig Bittencourt (ig.ibert@gmail.com)
 *		      Camila Nunes (camilapbnunes@hotmail.com)
 *	              Marcos Tadeu (zero.ufal@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *///GEN-END:copyright
package br.ufal.ic.forbile.agents.behaviours;

import jade.core.AID;
import jade.core.behaviours.OneShotBehaviour;
import jade.lang.acl.ACLMessage;
import java.io.IOException;
import java.io.Serializable;
import java.util.logging.Logger;

/**
 * GeneralActuator1.java
 *
 * <p>A full description of this class.
 *
 * @see SomeRelatedClass.
 *
 * @author <a href="mailto:ig.ibert@gmail.com">ig</a>.
 * @since 0.1
 * @version 0.1
 *
 */
public class GeneralActuator extends OneShotBehaviour {
    
    // Constants -----------------------------------------------------
    
    // Attributes ----------------------------------------------------
    
    private AID          receiver;
    private Serializable content;
    private ACLMessage   aCLMessage;
    private String       inReplyTo;
    
    // Static --------------------------------------------------------
    
    private static Logger logger;
    
    static {
        logger = Logger.getLogger(GeneralActuator.class.getName());
    }
    
    // Constructors --------------------------------------------------
    
    /**
     * Creates a new instance of GeneralActuator.
     *
     * @param receiver The AID receiver agent.
     * @param content The content to be sent.
     */
    public GeneralActuator(final AID receiver, final Serializable content) {
        this.receiver = receiver;
        this.content  = content;
    }
    
    /**
     * Creates a new instance of GeneralActuator.
     *
     * @param receiver The AID receiver agent.
     * @param aCLMessage The ACLMessage to be sent.
     */
    public GeneralActuator(final AID receiver, final ACLMessage aCLMessage) {
        this.receiver   = receiver;
        this.aCLMessage = aCLMessage;
    }
    
    /**
     * Creates a new instance of GeneralActuator.
     *
     * @param receiver The AID receiver agent.
     * @param content The content to be sent.
     * @param inReplyTo The request message type.
     */
    public GeneralActuator(final AID receiver, final Serializable content, final String inReplyTo) {
        this.receiver   = receiver;
        this.content = content;
        this.inReplyTo = inReplyTo;
    }
    
    // Public --------------------------------------------------------
    
    // Z implementation ----------------------------------------------
    
    // Y overrides ---------------------------------------------------
    
    public void action() {
        logger.info(myAgent.getLocalName() + " sending message to " + this.receiver.getLocalName());
        
        if (aCLMessage != null) {
            aCLMessage.addReceiver(this.receiver);
            this.myAgent.send(aCLMessage);
        } else {
            ACLMessage message = new ACLMessage(ACLMessage.INFORM);
            message.addReceiver(this.receiver);
            
            if (content instanceof String) {
                message.setContent(content.toString());
            } else {
                try {
                    message.setContentObject(content);
                } catch (IOException ex) {
                    logger.info("Message could not be sent");
                }
            }

            if (!(this.inReplyTo == null)) {
                message.setInReplyTo(this.inReplyTo);
            }

            this.myAgent.send(message);
        }
        
        this.myAgent.removeBehaviour(this);
    }
    
    // Package protected ---------------------------------------------
    
    // Protected -----------------------------------------------------
    
    // Private -------------------------------------------------------
    
    // Inner classes -------------------------------------------------
    
}
